package work;

import work.fram.*;
import work.person.helper.ConditionHelper;
import work.utils.TextUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 比对两个数据库将已存在的身份证号标记 {

    private static Statement[] getStatement() {

        Statement[] statements = new Statement[2];

        //当前库
        Statement statement = JDBCConnect.getStatement();

        statements[0] = statement;
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.2.251:3306/testconnect?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8", "glsh", "glsh");

            Statement connStatement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            statements[1] = connStatement;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return statements;
    }

    public static void main(String[] args) throws SQLException {

        Statement[] statement = getStatement();
        Statement current = statement[0];
        Statement target = statement[1];

        ResultSet resultSet = current.executeQuery("SELECT name,idcard,sysid FROM tc_md_personnel WHERE create_time=\"2019-12-03 00:00:00\" OR create_time=\"2019-12-03 15:00:00\" OR create_time=\"2019-12-04 0:00:00\" OR create_time=\"2019-12-04 13:00:00\"");

        //查询所有结果
        List<A> list = new ArrayList<>();
        while (resultSet.next()) {
            A a = new A();
            a.idcard = resultSet.getString("idcard");
            a.sysid = resultSet.getString("sysid");
            a.name = resultSet.getString("name");
            list.add(a);
        }

        SqlMaker sqlMaker = new SqlMaker("tc_md_personnel");
        KeyValueMapper mapper = sqlMaker.createKeyValueMapper();

        StringBuilder builder = new StringBuilder();

        int size = list.size();
        int j = 0;
        for (A a : list) {
            j++;

            System.out.println(j * 100.0 / size + "%");

            ResultSet query = target.executeQuery("select count(idcard) from tc_md_personnel where idcard='" + a.idcard + "'");
            if (query.next()) {
                int i = query.getInt(1);
                if (i > 0) {
                    mapper.reset();
                    mapper.put("create_time", "2019-12-01 00:00:00");

                    if (sqlMaker.make(SqlMaker.TYPE_UPDATE, ConditionHelper.create("sysid", "=", a.sysid))) {
                        builder.append(sqlMaker.getSql());
                        AutoSqlWriter.INSTANCE.write("更新sql", sqlMaker.getSql());
                        AutoLogger.getINTERFACE().writeLine("比对后变更时间戳", a.idcard, a.name);
                    }

                }
            }
        }
        AutoLogger.getINTERFACE().close();
        AutoSqlWriter.INSTANCE.close();

        TextUtils.textToClipboard(builder.toString());

    }

    private static class A {
        String idcard;
        String sysid;
        String name;

        @Override
        public String toString() {
            return idcard + "\t" + sysid;
        }
    }

}
